﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using LyyCMS.Core;
using LyyCMS.Core.Models;
using Microsoft.AspNetCore.Http;
using LyyCommon;
using Microsoft.Extensions.Logging;

namespace LyyCMS.Web.Areas.Dash.Controllers
{
    [Area("Dash")]
    [Route("Dash/[controller]/[action]")]
    [Filters.SignFilter]
    public class ArticleCategoriesController : BaseController
    {

        public ArticleCategoriesController(ILogger<BaseController> loggger, LyyCMS.Core.LyyCMSEntities lyyCMSEntities) : base(loggger, lyyCMSEntities)
        {
        }

        // GET: Dash/ArticleCategories
        public async Task<IActionResult> Index()
        {
            var categories = _context.ArticleCategories.Include(c => c.Channel);

            return View(await categories.ToListAsync());
        }

        // GET: Dash/ArticleCategories/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var articleCategory = await _context.ArticleCategories.Include(c=>c.Channel)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (articleCategory == null)
            {
                return NotFound();
            }

            return View(articleCategory);
        }

        // GET: Dash/ArticleCategories/Create
        public IActionResult Create()
        {
            //需要分类
            var cates = _context.ArticleCategories.Include(c=>c.Channel).ToList();
            if (cates.Any())
            {
                ViewBag.cate = cates;
            }
            else
            {
                cates = new List<ArticleCategory>()
                {
                    new ArticleCategory
                    {
                        Id=0,
                        Title="顶级"
                    }
                };
                ViewBag.cate = cates;
            }
            
            //频道
            var channel = _context.Channels.ToList();
            if (channel.Any())
            {
                ViewBag.channel = channel;
            }
            else
            {
                return RedirectToAction("Index");
            }
            
            return View();
        }

        // POST: Dash/ArticleCategories/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Title,ParentId,ChannelId,SortId,Id")] ArticleCategory articleCategory)
        {
            if (ModelState.IsValid)
            {
                base.GetSession(HttpContext);
                articleCategory.CreationTime = DateTime.Now;
                articleCategory.IsDeleted = false;
                articleCategory.CreatorUserId = session_uid;

                _context.Add(articleCategory);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(articleCategory);
        }

        // GET: Dash/ArticleCategories/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var articleCategory = await _context.ArticleCategories.FindAsync(id);
            if (articleCategory == null)
            {
                return NotFound();
            }
            return View(articleCategory);
        }

        // POST: Dash/ArticleCategories/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Title,ParentId,SortId,CreationTime,DeletionTime,IsDeleted,CreatorUserId,LastModifierUserId,LastModificationTime,DeleterUserId,Id")] ArticleCategory articleCategory)
        {
            if (id != articleCategory.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(articleCategory);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ArticleCategoryExists(articleCategory.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(articleCategory);
        }

        // GET: Dash/ArticleCategories/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var articleCategory = await _context.ArticleCategories
                .FirstOrDefaultAsync(m => m.Id == id);
            if (articleCategory == null)
            {
                return NotFound();
            }

            return View(articleCategory);
        }

        // POST: Dash/ArticleCategories/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var articleCategory = await _context.ArticleCategories.FindAsync(id);
            _context.ArticleCategories.Remove(articleCategory);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool ArticleCategoryExists(int id)
        {
            return _context.ArticleCategories.Any(e => e.Id == id);
        }
    }
}
